package org.dromara.activity.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.activity.domain.School;
import org.dromara.activity.domain.bo.SchoolBo;
import org.dromara.activity.domain.vo.SchoolVo;
import org.dromara.activity.mapper.SchoolMapper;
import org.dromara.activity.service.SchoolService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
* @author Administrator
* @description 针对表【t_school】的数据库操作Service实现
* @createDate 2024-01-16 09:59:09
*/
@Service
@RequiredArgsConstructor
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School>
    implements SchoolService{

    private final SchoolMapper baseMapper;

    /**
     * 学校分页查询
     * @param school 学校
     * @param pageQuery 分页
     * @return {@link TableDataInfo}<{@link SchoolVo}>
     */
    @Override
    public TableDataInfo<SchoolVo> selectPageSchoolList(SchoolBo school, PageQuery pageQuery) {
        LambdaQueryWrapper<School> lqw = buildQueryWrapper(school);
        Page<SchoolVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 校验学校名称
     * @param school 学校
     * @return boolean
     */
    @Override
    public boolean checkSchoolNameUnique(SchoolBo school) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<School>()
            .eq(School::getSchoolName, school.getSchoolName())
            .ne(ObjectUtil.isNotNull(school.getId()), School::getId, school.getId()));
        return !exist;
    }

    /**
     * 插入学校
     * @param bo 学校信息
     * @return int
     */
    @Override
    public int insertSchool(SchoolBo bo) {
        School school = MapstructUtils.convert(bo, School.class);
        // 新增学校信息
        return baseMapper.insert(school);
    }

    /**
     * 更新学校
     * @param bo 学校信息
     * @return int
     */
    @Override
    public int updateSchool(SchoolBo bo) {
        School school = MapstructUtils.convert(bo, School.class);
        // 修改学校信息
       return baseMapper.updateById(school);
    }

    @Override
    public int updateSchoolStatus(Integer id, Integer status) {
        return baseMapper.update(null,
            new LambdaUpdateWrapper<School>()
                .set(School::getStatus, status)
                .eq(School::getId, id));
    }

    /**
     * 删除学校
     * @param ids id集合
     * @return int
     */
    @Override
    public int deleteSchoolByIds(Long[] ids) {
        // todo 删除学校判断学校下有没有用户
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }


    private LambdaQueryWrapper<School> buildQueryWrapper(SchoolBo bo) {
        LambdaQueryWrapper<School> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSchoolName()), School::getSchoolName, bo.getSchoolName());
        lqw.orderByAsc(School::getId);
        return lqw;
    }
}




